﻿<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:w="http://schemas.jelle.druyts.net/2010/xaml/presentation"
             xmlns:si="clr-namespace:Schedulr.Infrastructure"
             xmlns:svm="clr-namespace:Schedulr.ViewModels"
             x:Class="Schedulr.Views.Controls.PictureQueue"
             d:DesignHeight="480"
             d:DesignWidth="640"
             mc:Ignorable="d"
             w:MergeCollections.InputBindings="{Binding InputBindings}">
    <UserControl.Resources>

        <si:SchedulrDragDropHandler x:Key="dragDropHandler" />

        <DataTemplate x:Key="DragAdorner">
            <Border Style="{DynamicResource BorderStyle}" Background="Transparent" Padding="5" Margin="5">
                <Image MaxWidth="100" MaxHeight="100">
                    <Image.Source>
                        <BitmapImage DecodePixelWidth="100" UriSource="{Binding ImageUrl}" />
                    </Image.Source>
                </Image>
            </Border>
        </DataTemplate>

        <DataTemplate DataType="{x:Type svm:PictureViewModel}">
            <Border x:Name="pictureBorder" Style="{StaticResource BorderStyle}" Background="Transparent" Padding="5" Margin="5">
                <Border.LayoutTransform>
                    <ScaleTransform ScaleX="{Binding Path=DataContext.Scale, ElementName=layoutRoot}" ScaleY="{Binding Path=DataContext.Scale, ElementName=layoutRoot}" />
                </Border.LayoutTransform>
                <w:BackgroundLoadingImage Source="/Resources/Picture.png" ActualSource="{Binding ImageUrl}" DecodePixelWidth="400" ToolTip="{Binding Path=Picture.Title}" MaxWidth="100" MaxHeight="100" />
            </Border>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True">
                    <Setter TargetName="pictureBorder" Property="BorderBrush" Value="{DynamicResource ImageBorderBorderBrush}" />
                    <Setter TargetName="pictureBorder" Property="Background" Value="{DynamicResource ImageBorderFillBrush}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding HasErrors}" Value="True">
                    <Setter TargetName="pictureBorder" Property="BorderBrush" Value="{DynamicResource ErrorForegroundBrush}" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>

    </UserControl.Resources>

    <DockPanel LastChildFill="True" x:Name="layoutRoot">
        <DockPanel DockPanel.Dock="Bottom" LastChildFill="True">
            <Border Style="{DynamicResource BorderStyle}" Margin="5">
                <WrapPanel Orientation="Horizontal" Margin="5,0,5,5">
                    <ItemsControl ItemsSource="{Binding Commands}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel Orientation="Horizontal" />
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Button Content="{Binding Name}" ToolTip="{Binding Description}" Command="{Binding}" Margin="0,5,5,0" />
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="0,5,0,0">
                        <TextBlock Text="Zoom" DockPanel.Dock="Left" VerticalAlignment="Center" Foreground="{DynamicResource TextBrush}" Margin="5,0,0,0" />
                        <Slider Width="100" Margin="10,0,10,0" Minimum="0.5" Maximum="4" Value="{Binding Scale}" LargeChange="0.5" SmallChange="0.1" TickPlacement="BottomRight" TickFrequency="0.5">
                            <Slider.LayoutTransform>
                                <ScaleTransform ScaleX="0.85" ScaleY="0.85" />
                            </Slider.LayoutTransform>
                        </Slider>
                    </StackPanel>
                </WrapPanel>
            </Border>
        </DockPanel>
        <ListBox x:Name="picturesListBox" Background="Transparent" BorderThickness="0"
                 ItemsSource="{Binding Pictures}" SelectionMode="Extended" SelectionChanged="picturesListBox_SelectionChanged"
                 Mouse.MouseDown="picturesListBox_MouseDown" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                 w:DragDrop.IsDragSource="{Binding IsDragDropSupported}" w:DragDrop.IsDropTarget="{Binding IsDragDropSupported}" w:DragDrop.DragAdornerTemplate="{StaticResource DragAdorner}" w:DragDrop.DropHandler="{StaticResource dragDropHandler}" w:DragDrop.CreateNewGroupOnHeaderDrop="True">
            <ListBox.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
                <DataTemplate x:Key="StretchedHeaderTemplate">
                    <Border Loaded="StretchedHeaderTemplate_Loaded" HorizontalAlignment="Stretch">
                        <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Stretch" />
                    </Border>
                </DataTemplate>
            </ListBox.Resources>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Expander HeaderTemplate="{StaticResource StretchedHeaderTemplate}" Margin="8,8,8,0">
                                            <Expander.Header>
                                                <TextBlock TextWrapping="NoWrap" HorizontalAlignment="Stretch" Text="{Binding Name}" />
                                            </Expander.Header>
                                            <ItemsPresenter />
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </ListBox.GroupStyle>
        </ListBox>
    </DockPanel>

</UserControl>